var Comet = Class.create();
Comet.prototype = {
    timestamp: 0,
    url: './procesar_chat.php',
    noerror: true,
    initialize: function(name){
        this.chatname = name;
    },
    connect: function(){
        this.ajax = new Ajax.Request(this.url, {
            method: 'get',
            parameters: {
                'timestamp' : this.timestamp,
                'chat' : this.chatname
            },
            onSuccess: function(transport) {
                // manejamos la respuesta del servidor
                var response = transport.responseText.evalJSON();
                this.comet.timestamp = response['timestamp'];
                this.comet.handleResponse(response);
                this.comet.noerror = true;
            },
            onComplete: function(transport) {
                // enviamos nueva petición ajax cuando esta haya finalizado
                if (!this.comet.noerror)
                    // si conexión tuvo problemas, intentar reconectar cada 5 segundos
                    setTimeout(function(){
                        comet.connect()
                    }, 5000);
                else
                    this.comet.connect();
                this.comet.noerror = false;
            }
        });
        this.ajax.comet = this;
    },
    disconnect: function(){ },
    handleResponse: function(response){
        var id = response['id'];
        var name = response['name'];
        var image = response['image'];
        var mensaje = response['msg'];
        
        //Construye la tabla
        var table = "<table width=\"300\">"
        table += "<tr><td rowspan=\"2\">";
        table += "<a href=\"perfil.php?id="+id+"\"><img src=\""+image+"\" height=\"30\"></a>";
        table += "</td>";
        table += "<td><b>"+name+":</b></td>";
        table += "<tr><td>"+mensaje+"</td></tr>";
        table+= "</table>";

        var chat = document.getElementById('chat');
        chat.innerHTML += table;
    },
    doRequest: function(message,username,chatname){
        new Ajax.Request(this.url, {
            method: 'get',
            parameters: {
                'msg' : message,
                'username' : username,
                'chat' : chatname
            }
        });
    }
}
var comet = new Comet();

//inicia la sala de conversacion
function iniciarSala(nombre){
    var divisionChat = document.getElementById('chatroom');
    divisionChat.style.display = 'inline';
    var divisionList = document.getElementById('chatlist');
    divisionList.style.display = 'none';
    document.getElementById('chatname').value = nombre;
    document.getElementById('titulo').innerHTML += (" - "+nombre);
    comet.initialize(nombre);
    comet.connect();
}

//funciones adicionales
function enviar(){
    var phrase = document.getElementById('phrase').value;
    var username = document.getElementById('username').value;
    var chatname = document.getElementById('chatname').value;
    comet.doRequest(phrase,username,chatname);
    document.getElementById('phrase').value='';
    return false;
}